package site.yunnong.atvris.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.exception.LocalAssert;
import site.yunnong.atvris.dao.domain.UserProfile;
import site.yunnong.atvris.dao.domain.UserProfileExample;
import site.yunnong.atvris.dao.mapper.UserProfileMapper;
import site.yunnong.atvris.service.UserProfileService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户统计信息
 *
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:37
 */
@Slf4j
@Service
public class UserProfileServiceImpl implements UserProfileService {

    @Resource
    private UserProfileMapper mapper;

    @Override
    public UserProfile getUserProfileByUuid(Long uuid) {
        UserProfileExample example = new UserProfileExample();
        example.createCriteria().andUuidEqualTo(uuid);
        List<UserProfile> userProfileList = mapper.selectByExample(example);
        LocalAssert.notEmpty(userProfileList, "数据异常，未查到用户统计数据");
        return userProfileList.get(0);
    }

    @Override
    public int insert(Long uuid) {
        UserProfile userProfile = UserProfile.builder().uuid(uuid).build();
        return mapper.insertSelective(userProfile);
    }

    @Override
    public int addVideoCount(UserProfile profile) {
        UserProfileExample userProfileExample = new UserProfileExample();
        userProfileExample.createCriteria().andUuidEqualTo(profile.getUuid());
        profile.setVideoCount(profile.getVideoCount() + 1);
        return mapper.updateByExampleSelective(profile, userProfileExample);
    }

    @Override
    public List<UserProfile> queryAll() {
        return mapper.selectByExample(new UserProfileExample());
    }

    @Override
    public int updateUserProfile(UserProfile userProfile) {
        return mapper.updateByPrimaryKeySelective(userProfile);
    }
}

